package com.xms.configuration.service;

import com.xms.configuration.entity.Settings;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.CreaterService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.StringUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;

/**
 * <p>
 * SettingCreaterService
 * </p>
 *
 * @author migo
 * @since 2021/10/22
 */
@CacheConfig(cacheNames = "settings", keyGenerator = "CacheKeyGenerator")
@Service
public class SettingCreaterService extends CreaterService<Settings> implements ISettingCreaterService {
    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean create(Settings entity) throws SQLException, XmsException {
        if (StringUtil.isEmpty(entity.getName())) {
            throw new XmsException("name is required");
        }
        return super.save(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean createMany(Collection<Settings> entityList) throws SQLException, XmsException {
        if (CollectionUtil.notEmpty(entityList)) {
            for (Settings entity : entityList) {
                this.create(entity);
            }
        }
        return true;
    }
}
